package cn.stl.org.controller;

import cn.stl.basic.util.AjaxResult;
import cn.stl.basic.util.PageList;
import cn.stl.org.domain.Department;
import cn.stl.org.query.DepartmentQuery;
import cn.stl.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService iDepartmentService;

@DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
    try {
        iDepartmentService.remove(id);
        return AjaxResult.createSuccess();
    } catch (Exception e) {
        e.printStackTrace();
        return AjaxResult.createError();
    }
}

@PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            iDepartmentService.patchDelete(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
}
//查询一个对象
@GetMapping("/{id}")
    public Department queryById(@PathVariable Long id){
    return iDepartmentService.loadById(id);
}
//根据id新增或修改
@PutMapping
@ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
    try {
        if (department.getId() == null) {
            iDepartmentService.save(department);
        } else {
            iDepartmentService.update(department);
        }
        return AjaxResult.createSuccess();
    } catch (Exception e) {
        e.printStackTrace();
        return AjaxResult.createError();
    }
}
@GetMapping
    public List<Department> queryAll(){
    return iDepartmentService.loadAll();
}
@GetMapping("/tree")
    public List<Department> queryTree(){
    return iDepartmentService.queryTree();
}
@PostMapping
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
            return iDepartmentService.queryData(departmentQuery);
}
}
